package com.xm2.demo.system.shop.controller;

import com.xm2.demo.model.Stores;
import com.xm2.demo.system.shop.dto.StoreInfo;
import com.xm2.demo.system.shop.dto.StoresDto;
import com.xm2.demo.system.shop.dto.StoresPage;
import com.xm2.demo.system.shop.service.ShopService;
import com.xm2.demo.utils.JsonResult;
import com.xm2.demo.utils.Pagination;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Create
 * @create 2018-12-12 16:24
 * @Description：
 */
@Controller
@RequestMapping(value = "/system/")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @GetMapping(value = "shop")
    public String toShop(Model model) {
        Pagination pagination = new Pagination();
        pagination = shopService.findByPageable(pagination);
        model.addAttribute("pagination", pagination);
        return "shop";
    }

    @PostMapping(value = "shop")
    @ResponseBody
    public Pagination doShop(@RequestBody StoresPage storesPage){
        Pagination pagination;
        if (storesPage.getStoresName().equals("")){
            pagination = shopService.findByPageable(storesPage);
            pagination.setList(getStoresDtoList((List<Stores>) pagination.getPage().getContent()));
        } else {
            pagination = shopService.findByStoresPage(storesPage);
            pagination.setList( getStoresDtoList((List<Stores>) pagination.getList()));

        }
        return pagination;
    }

    //获取storesDto封装类的集合
    private static List<StoresDto> getStoresDtoList(List<Stores> list){
        List<StoresDto> storesDtoList = new ArrayList<>();
        for (Stores stores : list) {
            StoresDto storesDto = new StoresDto();
            BeanUtils.copyProperties(stores,storesDto);
            storesDto.setCenterName(stores.getCenter().getCenterName());
            storesDtoList.add(storesDto);
        }
        return storesDtoList;
    }
    /*
     * description:新增门店信息
     * @params [storeInfo]
     * @return com.xm2.demo.utils.JsonResult
     */
    @PostMapping("addStores")
    @ResponseBody
    public JsonResult addStore(@RequestBody StoreInfo storeInfo) {
        if (storeInfo.getStoresName().equals("")) {
            return new JsonResult("101", "洗衣门店名不能为空!");
        } else if (storeInfo.getStoresAddress().equals("")) {
            return new JsonResult("102", "地址不能为空!");
        } else if (storeInfo.getCenterName() == null) {
            return new JsonResult("103", "没有可选择的洗衣中心!");
        } else {
            shopService.addStore(storeInfo);
            return new JsonResult();
        }
    }

    /*
     * description:批量删除
     * @params [ids]
     * @return com.xm2.demo.utils.JsonResult
     */
    @PostMapping(value = "deleteStoresByIds")
    @ResponseBody
    public JsonResult deleteIds(@RequestBody String ids) {
        try {
            shopService.deleteByIds(ids);
            return new JsonResult();
        } catch (Exception e) {
            return new JsonResult("104", "删除失败");
        }
    }

    /*
     * description:更新门店信息
     * @params [storesDto]
     * @return com.xm2.demo.utils.JsonResult
     */
    @PostMapping("updateStores")
    @ResponseBody
    public JsonResult update(@RequestBody StoresDto storesDto) {
        if (storesDto.getCenterName().equals("")) {
            return new JsonResult("105", "洗衣中心名不能为空!");
        } else if (storesDto.getStoresName().equals("")) {
            return new JsonResult("101", "洗衣门店名不能为空!");
        } else if (storesDto.getStoresAddress().equals("")) {
            return new JsonResult("102", "地址不能为空!");
        } else {
            shopService.update(storesDto);
        }
        return new JsonResult();
    }
}
